﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.Utils.Menu;
using System.Windows.Forms;
using System.ComponentModel;

namespace ProgNetComponentsDx.GridTools
{
    public class GridToolsProvider : Base.Components.BaseExtenderProvider<GridToolsSettings>
    {
        public GridToolsProvider()
        {
            Init();
        }

        public GridToolsProvider(IContainer container)
            : base(container)
        {
            Init();
        }
        private void Init()
        {
            ExportMenucaption = "Eksportuj";
        }

        protected override void ProcessObject(object target, GridToolsSettings settings)
        {
            if (!this.IsDesignMode())
            {
                GridView gv = target as GridView;
                if (gv != null)
                {
                    gv.PopupMenuShowing -= new PopupMenuShowingEventHandler(gv_PopupMenuShowing);
                    gv.PopupMenuShowing += new PopupMenuShowingEventHandler(gv_PopupMenuShowing);
                }
            }
        }

        void gv_PopupMenuShowing(object sender, PopupMenuShowingEventArgs e)
        {
            GridView gv = sender as GridView;
            object key = GetRealKey(sender);
            if (AllObjects.ContainsKey(key))
            {
                GridToolsSettings s = AllObjects[key];
                DXMenuItem item;
                if (s.AddExportMenu)
                {
                    DevExpress.Utils.Menu.DXSubMenuItem exportItem = new DevExpress.Utils.Menu.DXSubMenuItem(ExportMenucaption);

                    if (s.ExportSettings.ExportToHtml)
                    {
                        item = new DXMenuItem("HTML", ExportToHtml);
                        item.Tag = gv;
                        exportItem.Items.Add(item);
                    }
                    if (s.ExportSettings.ExportToPdf)
                    {
                        item = new DXMenuItem("PDF", ExportToPdf);
                        item.Tag = gv;
                        exportItem.Items.Add(item);
                    }
                    if (s.ExportSettings.ExportToXls)
                    {
                        item = new DXMenuItem("XLS", ExportToXls);
                        item.Tag = gv;
                        exportItem.Items.Add(item);
                    }
                    if (s.ExportSettings.ExportToXlsx)
                    {
                        item = new DXMenuItem("XLSX", ExportToXlsx);
                        item.Tag = gv;
                        exportItem.Items.Add(item);
                    }
                    if (exportItem.Items.Count > 0)
                        e.Menu.Items.Add(exportItem);
                    else
                        exportItem.Dispose();
                }
            }
        }
        public string ExportMenucaption
        {
            get;
            set;
        }

        #region Export
        private void Export(object sender, ExportType type)
        {
            DXMenuItem item = sender as DXMenuItem;
            if (item != null && item.Tag != null && item.Tag is GridView)
            {
                GridView gv = item.Tag as GridView;
                object key = GetRealKey(gv);
                if (AllObjects.ContainsKey(key))
                {
                    GridToolsSettings s = AllObjects[key];
                    string file = GetFileName(type, s);
                    if (file.IsNotNull())
                    {
                        bool zoommed = gv.IsZoomedView;
                        try
                        {
                            if (!zoommed)
                                gv.ZoomView();
                            switch (type)
                            {
                                case ExportType.Html:
                                    gv.ExportToHtml(file);
                                    break;
                                case ExportType.Pdf:
                                    gv.ExportToPdf(file);
                                    break;
                                case ExportType.Xls:
                                    gv.ExportToXls(file);
                                    break;
                                case ExportType.Xlsx:
                                    gv.ExportToXlsx(file);
                                    break;
                                default:
                                    break;
                            }
                        }
                        catch (Exception ex)
                        {
                            Form.ActiveForm.ShowDxErrorDialog("Wystąpił problem podczas eksportu:\n" + ex.Message);
                        }
                        finally
                        {
                            if (!zoommed)
                                gv.NormalView();
                        }
                    }
                }
            }
        }
        private void ExportToHtml(object sender, EventArgs e)
        {
            Export(sender, ExportType.Html);
        }
        private void ExportToPdf(object sender, EventArgs e)
        {
            Export(sender, ExportType.Pdf);
        }
        private void ExportToXls(object sender, EventArgs e)
        {
            Export(sender, ExportType.Xls);
        }
        private void ExportToXlsx(object sender, EventArgs e)
        {
            Export(sender, ExportType.Xlsx);
        }

        private string GetFileName(ExportType type, GridToolsSettings s)
        {
            string file = string.Empty;
            using (SaveFileDialog dlg = new SaveFileDialog())
            {
                string ext = type.ToString().ToLower();
                dlg.CheckFileExists = false;
                dlg.DefaultExt = "." + ext;
                dlg.Filter = ext + "|*." + ext;
                dlg.FileName = s.FileName + "." + ext;
                dlg.AddExtension = true;
                if (dlg.ShowDialog() == DialogResult.OK)
                    file = dlg.FileName;
            }
            return file;
        }
        #endregion

        protected override GridToolsSettings CreateSettings(object target)
        {
            return new GridToolsSettings(target, this);
        }

        public override bool CanExtend(object extendee)
        {
            return (extendee is GridView);
        }
        protected override object GetRealKey(object target)
        {
            object key = target;

            if (target is GridView)
            {
                GridView gv = target as GridView;
                if (gv.SourceView != null)
                    key = gv.SourceView;
            }
            return key;
        }

        private enum ExportType
        {
            Pdf,
            Xls,
            Xlsx,
            Html
        }
    }
}
